package com.lv.service.room.address

import android.content.Context
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

/**
 * 作者： ygx
 * 创建日期：2022/1/8 2:42 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 *      _              _           _     _   ____  _             _ _
 *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 *
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * cniao数据库操作帮助类
 */
object LocationNewReceiptAddressDbHelper {

    /**
     * 获取room数据表中存储的userInfo
     * return liveData形式
     */
    fun getLiveLocation(context: Context, id: Int = 0) =
        LocationNewReceiptAddressDatabase.getInstance(context).locationDao.queryLiveLocation(id)

    /**
     * 获取room数据表中存储的userInfo
     * return liveData形式
     */
    fun getLiveAllLocation(context: Context) =
        LocationNewReceiptAddressDatabase.getInstance(context).locationDao.queryLiveAllLocation()

    /**
     * 以普通数据对象的形式，获取userInfo
     */
//    fun getLocation(context: Context, id: Int = 0) =
//        LocationSelectedDatabase.getInstance(context).locationDao.queryFindCategory(id)

    /**
     * 以普通数据对象的形式，获取userInfo
     */
//    fun getAllLocation(context: Context) =
//        LocationSelectedDatabase.getInstance(context).locationDao.queryFindAllCategory()

    /**
     * 以普通数据对象的形式，获取userInfo
     */
    suspend fun getLocationSuspend(context: Context) =
        LocationNewReceiptAddressDatabase.getInstance(context).locationDao.getLocation()

    /**
     * 以普通数据对象的形式，获取userInfo
     */
    suspend fun getAllLocationSuspend(context: Context) =
        LocationNewReceiptAddressDatabase.getInstance(context).locationDao.getAllLocation()

    /**
     * 删除数据表中的userInfo信息
     */
    fun deleteLocation(context: Context) {
        GlobalScope.launch(Dispatchers.IO) {
            getLocationSuspend(context)?.let { info ->
                LocationNewReceiptAddressDatabase.getInstance(context).locationDao.deleteLocation(
                    info
                )
            }
        }
    }

    /**
     * 删除数据表中的所有userInfo信息
     */
    fun deleteAllLocation(context: Context) {
        GlobalScope.launch(Dispatchers.IO) {
            LocationNewReceiptAddressDatabase.getInstance(context).locationDao.deleteAllLocation()
        }
    }

    /**
     * 以普通数据对象的形式，获取userInfo
     */
    suspend fun deleteSuspendAllLocation(context: Context) =
        LocationNewReceiptAddressDatabase.getInstance(context).locationDao.deleteSuspendAllLocation()

    /**
     * 新增用户数据到数据表
     */
    fun insertLocation(context: Context, addressBeanItem: ReceiptAddressItem) {
        GlobalScope.launch(Dispatchers.IO) {
            LocationNewReceiptAddressDatabase.getInstance(context).locationDao.insertLocation(
                addressBeanItem
            )
        }
    }

    /**
     * 以普通数据对象的形式，获取userInfo
     */
    suspend fun insertSuspendLocation(context: Context, addressBeanItem: ReceiptAddressItem) =
        LocationNewReceiptAddressDatabase.getInstance(context).locationDao.insertSuspendLocation(
            addressBeanItem
        )

    /**
     * 以普通数据对象的形式，获取userInfo
     */
    suspend fun insertAllLocation(context: Context, addressBeanItem: List<ReceiptAddressItem>) {
        LocationNewReceiptAddressDatabase.getInstance(context).clearAllTables()
        LocationNewReceiptAddressDatabase.getInstance(context).locationDao.insertSuspendLocation(
            addressBeanItem
        )
    }

}